package ysc.cqnu.operdb.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import ysc.cqnu.operdb.entity.ResultCaijian;
import ysc.cqnu.operdb.entity.ResultData;
import ysc.cqnu.operdb.mapper.CaiJianMapper;
import ysc.cqnu.operdb.service.ZZCaiJianInfoInsertService;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ZZCaiJianInfoInsertServiceImpl implements ZZCaiJianInfoInsertService {
    @Autowired
    private ResultData resultData;
    @Autowired
    private ResultCaijian resultCaijian;
    @Autowired
    private CaiJianMapper caiJianMapper;
    @Override
    public ResultData zZCaiJianInfoInsertService(String caijian, String renwu, String yongtu, String juti, MultipartFile tupian, MultipartFile shipin, String miaoshu,String uploader) {
        String fileName = tupian.getOriginalFilename();
        String fileName1 = shipin.getOriginalFilename();
        //判断是否是.***格式
        int start = fileName.indexOf(".");
        if (start==-1){
            resultData.flag="typeError";
            resultData.data=null;
            return resultData;
        }

        //判断是不是图片
        String type = fileName.substring(start+1);
        if((!"jpg".equals(type))&&(!"png".equals(type))&&(!"bmp".equals(type))&&(!"JPG".equals(type))&&(!"jpeg".equals(type))){
            resultData.flag="typeError";
            resultData.data=null;
            return resultData;
        }

        //判断是否是.***格式
        int start1 = fileName1.indexOf(".");
        if (start1==-1){
            resultData.flag="typeError";
            resultData.data=null;
            return resultData;
        }

        //判断是不是视频
        String type1 = fileName1.substring(start1+1);
        if(!("mp4".equals(type1))){
            resultData.flag="typeError";
            resultData.data=null;
            return resultData;
        }
        //图片存入的绝对路径
        String filePath = "E://up360/apache-tomcat-9.0.37/webapps/drawingImgZZ/";
        File targetFile = new File(filePath);

        //将图片命名为 用户名.***
        fileName = uploader+"."+type;

        if(!targetFile.exists()){
            targetFile.mkdirs();
        }

        FileOutputStream out = null;
        try {
            out = new FileOutputStream(filePath+fileName);
            out.write(tupian.getBytes());
            out.flush();
            out.close();

        } catch (Exception e) {
            resultData.flag="CaijianInfoFail";
            resultData.data=null;
            return resultData;
        }
        //视频存入的绝对路径
        String filePath1 = "E://up360/apache-tomcat-9.0.37/webapps/drawingVideoZZ/";
        File targetFile1 = new File(filePath1);

        //将视频命名为 用户名+.***
        fileName1 =uploader +type1;

        if(!targetFile1.exists()){
            targetFile1.mkdirs();
        }

        FileOutputStream out1 = null;
        try {
            out1 = new FileOutputStream(filePath1+fileName1);
            out1.write(shipin.getBytes());
            out1.flush();
            out1.close();
            String Image1Src="http://192.168.43.161:8080/drawingVideoZZ/"+fileName1;
            String ImageSrc="http://192.168.43.161:8080/drawingImgZZ/"+fileName;
            resultCaijian.caijian=caijian;
            resultCaijian.renwu=renwu;
            resultCaijian.yongtu=yongtu;
            resultCaijian.juti=juti;
            resultCaijian.miaoshu=miaoshu;
            resultCaijian.ImageSrc=ImageSrc;
            resultCaijian.Image1Src=Image1Src;
            caiJianMapper.insertCaijianInfoZZ(caijian,renwu,yongtu,juti,ImageSrc,Image1Src,miaoshu);
            resultData.flag="CaijianInfoSuccess";
            resultData.data=resultCaijian;
        } catch (Exception e) {
            e.printStackTrace();
            resultData.flag="CaijianInfoFail";
            resultData.data=null;
            return resultData;
        }
        return resultData;
    }
}
